package charts ;

import flash.events.Event;
import flash.events.MouseEvent;
import charts.series.Element;
import charts.series.dots.Scat;
import string.Utils;
import flash.geom.Point;
import flash.display.Sprite;
import flash.display.BlendMode;
import charts.series.dots.DefaultDotProperties;
import charts.ScatterBase.SCATTERLINESTRUCT;
import ColorConverter.Color;

class ScatterLine extends ScatterBase
{
	public var stepgraph: Float;
	public static inline var STEP_HORIZONTAL: Float = 1;
	public static inline var STEP_VERTICAL: Float = 2;
	//var style : ScatterBase.SCATTERLINESTRUCT;

	public function new ( json: Dynamic )
	{
		super();

		stepgraph = 0;
		//
		// so the mask child can punch a hole through the line
		//
		this.blendMode = BlendMode.LAYER;
		//
		
		style = {
			values:			[],
			width:			2,
			color:			Hex('#3030d0'),
			text:			'',		// <-- default not display a key
			font_size:		12,
			stepgraph:		0,
			axis:			'left'
		};
		
		// hack: keep this incase the merge kills it, we'll
		// remove the merge later (and this hack)
		var tmp: Dynamic = Reflect.field(json,'dot-style');
		
		ObjectHelper.merge_2( json, style );
		
		this.default_style = new DefaultDotProperties(
			Reflect.field(json,'dot-style')
			, ColorConverter.toString(this.style.color)
			, this.style.axis
		);
		
		this.line_width = style.width;
		this.color = ColorConverter.toInt(this.style.color);
		this.key = style.text;
		this.font_size	= style.font_size;
		//this.circle_size = style['dot-size'];
		
		switch (style.stepgraph) {
			case 'horizontal':
				stepgraph = STEP_HORIZONTAL;
				/* break */
			case 'vertical':
				stepgraph = STEP_VERTICAL;
				/* break */
		}

		this.values = style.values;
		this.add_values();
	}
	

	
	// Draw points...
	public override function resize ( sc:ScreenCoordsBase ): Void {
		
		// move the dots:
		super.resize( sc );
		
		this.graphics.clear();
		this.graphics.lineStyle( this.style.width, ColorConverter.toFloat(this.style.color) );
		
		//if( this.style['line-style'].style != 'solid' )
		//	this.dash_line(sc);
		//else
		this.solid_line(sc);
			
	}
	
	//
	// This is cut and paste from LineBase
	//
	public function solid_line( sc:ScreenCoordsBase ): Void {
		
		var first:Bool = true;
		var last_x: Float = 0;
		var last_y: Float = 0;

		var areaClosed:Bool = true;
		var isArea:Bool = false;
		var areaBaseX: Float = Math.NaN;
		var areaBaseY: Float = Math.NaN;
		var areaColor: Int = ColorConverter.toInt(Hex(this.color));
		var areaAlpha: Float = 0.4;
		var areaStyle: Dynamic = this.style.area_style;
		if (areaStyle != null)
		{
			isArea = true;
			if (areaStyle.x != null)
			{
				areaBaseX = areaStyle.x;
			}
			if (areaStyle.y != null)
			{
				areaBaseY = areaStyle.y;
			}
			if (areaStyle.color != null)
			{
				areaColor = ColorConverter.toInt(Hex(areaStyle.color)); //string.Utils.get_color( areaStyle.color );
			}
			if (areaStyle.alpha != null)
			{
				areaAlpha = areaStyle.alpha;
			}
			if (!Math.isNaN(areaBaseX)) 
			{
				// Convert X Value to screen position
				areaBaseX = sc.get_x_from_val(areaBaseX);
			}
			if (!Math.isNaN(areaBaseY)) 
			{
				// Convert Y Value to screen position
				areaBaseY = sc.get_y_from_val(areaBaseY);  // TODO: Allow for right Y-Axis??
			}
		}
		
		for ( i in 0...this.numChildren ) {
			
			var tmp:Sprite = cast(this.getChildAt(i),Sprite);
			
			
			//tr.ace("!!");
			//
			// filter out the line masks
			//
			if(Std.is(tmp,Element ))
			{
				var e:Element = cast(tmp,Element);
				
				//tr.ace(e.x);
				
				// tell the point where it is on the screen
				// we will use this info to place the tooltip
				e.resize( sc );
				if (!e.visible)
				{
					// Creates a gap in the plot and closes out the current area if defined
					if ((isArea) && (i > 0))
					{
						// draw an invisible line back to the base and close the fill
						//areaX = Math.isNaN(areaBaseX) ? last_x : areaBaseX;
						//areaY = Math.isNaN(areaBaseY) ? last_y : areaBaseY;
						var areaX:Float = last_x;
						var areaY:Float = last_y;
						this.graphics.lineStyle( 0, areaColor, 0 );
						this.graphics.lineTo(areaX, areaY);
						this.graphics.endFill();
						areaClosed = true;
					}
					first = true;
				}
				else if( first )
				{
					if (isArea)
					{
						// draw an invisible line from the base to the point
						var areaX: Float = Math.isNaN(areaBaseX) ? e.x : areaBaseX;
						var areaY: Float = Math.isNaN(areaBaseY) ? e.y : areaBaseY;
						// Begin the fill for the area
						this.graphics.beginFill(areaColor, areaAlpha);
						this.graphics.lineStyle( 0, areaColor, 0 );
						this.graphics.moveTo(areaX, areaY);
						this.graphics.lineTo(e.x, e.y);
						areaClosed = false;
						// change the line style back to normal
						this.graphics.lineStyle( this.style.width, ColorConverter.toFloat(this.style.color), 1.0 );
					}
					else
					{
						// just move to the point
						this.graphics.moveTo(e.x, e.y);
					}
					first = false;
				}
				else
				{
					if (this.stepgraph == STEP_HORIZONTAL)
						this.graphics.lineTo(e.x, last_y);
					else if (this.stepgraph == STEP_VERTICAL)
						this.graphics.lineTo(last_x, e.y);
				
					this.graphics.lineTo(e.x, e.y);
				}
				last_x = e.x;
				last_y = e.y;
			}
		}

		// Close out the area if defined
		if (isArea && !areaClosed)
		{
			// draw an invisible line back to the base and close the fill
			var areaX:Float = if (Math.isNaN(areaBaseX))  last_x else areaBaseX;
			var areaY:Float = if (Math.isNaN(areaBaseY)) last_y else areaBaseY;
			this.graphics.lineStyle( 0, areaColor, 0 );
			this.graphics.lineTo(areaX, areaY);
			this.graphics.endFill();
		}
	}
	
}
